<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">

          <a-col :md="8" :sm="24">
            <a-form-item label="采购单号">
              <a-input v-model="queryParam.orderNumber" placeholder="请输入采购单号"/>
            </a-form-item>
          </a-col>
					<a-col :md="8" :sm="24">
						<a-form-item label="状态">
							<div
								style="display: flex;align-items: center;cursor: pointer;border-radius: 5px;width: 100%;border: 1px solid #ccc;">
								<div v-for="(item, index) in executeStatusList" @click="statusClick(item, index)" :key="index"
									:class="{ 'active': statusIndex == index }" style="width: 25%;text-align: center;">
									<div style="height: 32px;line-height: 32px;"
										:style="{ 'border-left': index == 0 ? null : '1px solid #ccc' }">
										{{ item.name }}
									</div>
								</div>
							</div>
						</a-form-item>
						</a-col>
          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <!--<a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>-->
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="true"
      :rowKey="(record) => record.code"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
    >
		<span slot="executeStatus" slot-scope="text, record">
				<span v-if="record.executeStatus == '1479467316549017601'">
					<span v-if="record.cgRkStatus == 1">待入库</span>
					<span v-else>入库中</span>
				</span>
				<span v-if="record.executeStatus == '1510546430701674498'">
					入库完成
				</span>
				<span v-if="record.executeStatus == '1479467391929049090'">
					已完结
				</span>
			</span>
      <span slot="action" slot-scope="text, record">
        <!-- <a-popconfirm v-if="isCanFinish(record) && isOne()"
                      placement="topRight" title="确认进行完结操作吗？"
                      @confirm="() => finish(record)">
          <a>完结</a>
        </a-popconfirm>
        <a-divider v-if="isCanFinish(record)"
                   type="vertical"/>
        <a @click="$refs.purchaseDetail.show(record)">发货明细</a> -->
				<a-popconfirm v-if="isCanFinish(record) && isOne()" placement="topRight" title="确认进行完结操作吗？"
					@confirm="() => finish(record)">
					<a>完结</a>
				</a-popconfirm>
				<a-divider v-if="isCanFinish(record)" type="vertical" />
				<a @click="$refs.detailForm1.rukuDetails(record,
					{ materialType })">入库明细</a>
				<a-divider type="vertical" />
				<a @click="$refs.rukuList.show(record,
					{ materialType, orderType })">入库记录</a>
      </span>

    </s-table>

    <purchase-detail ref="purchaseDetail"/>
		<detail-form1 ref="detailForm1" />
		<ruku-list ref="rukuList" />
  </a-card>
</template>

<script>
import {STable} from '@/components'
import {page, purchaseOrderFinish} from '@/api/modular/main/emsorder/emsOrderManage'
import PurchaseDetail from './purchaseDetail'


import detailForm1 from '../../../materialManagement/purchaseOrderByWaitInStore/detailForm1'
import rukuList from '../../../materialManagement/purchaseOrderByWaitInStore/rukuList'

export default {
  components: {
    PurchaseDetail,
    STable,
		
		detailForm1,
		rukuList
  },
  data() {
    return {
      advanced: false, // 高级搜索 展开/关闭
      queryParam: {},  // 查询参数
			executeStatusList: [
				{
					id: null,
					name: '全部',
				},
				{
					id: '1',
					name: '待入库',
					executeStatus: "1479467316549017601"
				},
				{
					id: '2',
					name: '入库中',
					executeStatus: "1479467316549017601"
				},
				{
					id: '1510546430701674498',
					name: '入库完成',
				},
				{
					id: '1479467391929049090',
					name: '已完结',
				}
			],
			statusIndex: 0,
      columns: [
        {
          title: '序号', width:'80px',
          customRender: (text, record, index) => `${index + 1}`
        },
        {
          title: '采购单号',
          dataIndex: 'orderNumber'
        },
        {
          title: '供应商',
          dataIndex: 'targetOrgName'
        },
        // {
        //   title: '采购数量',
        //   dataIndex: '',
        //   customRender: (text, record, index) => `【没算】`
        // },
        // {
        //   title: '已发数量',
        //   dataIndex: '',
        //   customRender: (text, record, index) => `【没算】`
        // },
        // {
        //   title: '状态',
        //   dataIndex: 'executeStatus',
        //     customRender: (text, record, index) => this.$util.findExecuteStatusById(text).name
        // },
				{
					title: '状态',
					dataIndex: 'executeStatus',
					scopedSlots: { customRender: 'executeStatus' }
				},
        {
          title: '采购时间',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
					align: 'center',
          width: '250px',
          scopedSlots: {customRender: 'action'}
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.queryParam.materialType = this.materialType
        this.queryParam.orderType = this.orderType
        // this.queryParam.executeStatus = this.executeStatus
        return page(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      scheduleProductionVisible: false,
      selectedRowKeys: [],
      selectedRows: [],
      defaultExpandedKeys: [],
      replaceFields: {
        key: 'id',
      },
    }
  },
  computed: {
    materialType() {
      return this.$businessEnum.MATERIAL_TYPE_ENUM.packing.id
    },
    orderType() {
      return this.$businessEnum.ORDER_TYPE_ENUM.purchaseOrder.id
    },
    executeStatus() {
      return this.$businessEnum.EXECUTE_STATUS_ENUM.IN_STORE_COMPLETED.id
    },
  },
  methods: {
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
		/**
		 * 状态切换
		 * @param item 
		 */
		 statusClick(item, index) {
			console.log(item);
			this.statusIndex = index;
			this.$nextTick(() => {
				if (item.id == 1 || item.id == 2) {
					this.queryParam.executeStatus = item.executeStatus;
					this.queryParam.cgRkStatus = item.id;
				} else {
					this.queryParam.executeStatus = item.id;
					this.queryParam.cgRkStatus = null;
				}
			})
		},
    isCanFinish(record) {
      return record.executeStatus == this.$businessEnum.EXECUTE_STATUS_ENUM.IN_STORE_COMPLETED.id
    },
    // 判断是不是总经理
    isOne() {
      return this.hasRole(this.$util.ROLE_ENUM.GENERALMANAGER.id)
    },
    finish(record) {
      purchaseOrderFinish(record).then(res => {
        if (res.success) {
          this.$message.success(res.message)
          this.handleOk()
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.active {
	background-color: #1890FF;
	color: #fff;
}
</style>